<?php
	/**
	 +-------------------------------------------------
	 * 去除二维数组中重复的数组，并保留键值
	 * @param array $array2D 数组
	 * @param boolean $stkeep 是否保留一级数组键
	 * @param boolean $ndformat 是否保留二级数组键
	 * @return array
	 +-------------------------------------------------
	 */
	function unique_arr($array2D, $stkeep = false, $ndformat = true) {
		// 判断是否保留一级数组键 (一级数组键可以为非数字)
		if ($stkeep)
			$stArr = array_keys($array2D);
	
		// 判断是否保留二级数组键 (所有二级数组键必须相同)
		if ($ndformat)
			$ndArr = array_keys(end($array2D));
	
		//降维,也可以用implode,将一维数组转换为用逗号连接的字符串
		foreach ($array2D as $v) {
			$v = join(",", $v);
			$temp[] = $v;
		}
	
		//去掉重复的字符串,也就是重复的一维数组
		$temp = array_unique($temp);
	
		//再将拆开的数组重新组装
		foreach ($temp as $k => $v) {
			if ($stkeep)
				$k = $stArr[$k];
			if ($ndformat) {
				$tempArr = explode(",", $v);
				foreach ($tempArr as $ndkey => $ndval)
					$output[$k][$ndArr[$ndkey]] = $ndval;
			} else
				$output[$k] = explode(",", $v);
		}
	
		return $output;
	}
	
	function cn_substr($str, $length, $start = 0) {
		if (strlen($str) < $start + 1) {
			return '';
		}
		preg_match_all("/./su", $str, $ar);
		$str = '';
		$tstr = '';
		//为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取
		for ($i = 0; isset($ar[0][$i]); $i++) {
			if (strlen($tstr) < $start) {
				$tstr .= $ar[0][$i];
			} else {
				if (strlen($str) < $length + strlen($ar[0][$i])) {
					$str .= $ar[0][$i];
				} else {
					break;
				}
			}
		}
		return $str;
	}
	
	/**
	 * =============================
	 * 获得上级分类名称
	 * @param INT $id 分类ID
	 * =============================
	 */
	function getParentCategory($id) {
		if ($id == 0) {
			return '无上级分类';
		}
		if ($list = F('categoryName')) {
			return $list[$id];
		}
		$dao = D("Category");
		$list = $dao->field('id,name')->select();
		foreach ($list as $vo) {
			$nameList[$vo['id']] = $vo['name'];
		}
		$name = $nameList[$id];
		F('categoryName', $nameList);
		return $name;
	}
	
	function getStatus($status, $imageShow = true) {
		$path = C('TMPL_PARSE_STRING');
		switch ($status) {
			case 0 :
				$showText = '禁用';
				//$showImg = '<IMG SRC="'.$path['__THEMES__'].'/images/locked.png" BORDER="0" ALT="禁用">';
				$showImg = '&Theta;';
				break;
			case 2 :
				$showText = '待审';
				//$showImg = '<IMG SRC="'.$path['__THEMES__'].'/images/prected.png" BORDER="0" ALT="待审">';
				$showImg = '※';
				break;
			case - 1 :
				$showText = '删除';
				//$showImg = '<IMG SRC="'.$path['__THEMES__'].'/images/del.png" BORDER="0" ALT="删除">';
				$showImg = '&Chi;';
				break;
			case 1 :
			default :
				$showText = '正常';
				$showImg = '&radic;';
				//$showImg = '<IMG SRC="'.$path['__THEMES__'].'/images/ok.png" BORDER="0" ALT="正常">';
		}
		return ($imageShow === true) ? $showImg : $showText;
	
	}
	
	/**
	 * =====================================================
	 * 判断状态，并按状态值返回链接
	 * @param INT $status 状态值 0 禁用 -1 已删除 1 正常 2未批准
	 * @param INT $id 数据条目ID
	 * @param string $callback JS回调值。默认空。
	 * =====================================================
	 */
	function showStatus($status, $id, $callback = "") {
		if (isset($_GET['tag'])) {
			switch ($status) {
				case 0 :
					$info = '<a href="' . U('resume?id=' . $id . '&tag=' . $_GET['tag']) . '">恢复</a>';
					break;
				case 2 :
					$info = '<a href="' . U('pass?id=' . $id . '&tag=' . $_GET['tag']) . '">批准</a>';
					break;
				case 1 :
					$info = '<a href="' . U('forbid?id=' . $id . '&tag=' . $_GET['tag']) . '">禁用</a>';
					break;
				case - 1 :
					$info = '<a href="' . U('recycle?id=' . $id . '&tag=' . $_GET['tag']) . '">还原</a>';
					break;
			}
		} else {
			switch ($status) {
				case 0 :
					$info = '<a href="' . U('resume?id=' . $id) . '">恢复</a>';
					break;
				case 2 :
					$info = '<a href="' . U('pass?id=' . $id) . '">批准</a>';
					break;
				case 1 :
					$info = '<a href="' . U('forbid?id=' . $id) . '">禁用</a>';
					break;
				case - 1 :
					$info = '<a href="' . U('recycle?id=' . $id) . '">还原</a>';
					break;
			}
		}
		return $info;
	}
	
	/* 状态显示 */
	function showRecommend($recommend, $id) {
	    switch ($recommend) {
	        case 0:$info = '<a href="javascript:recommend(' . $id . ')">推荐</a>';
	            break;
	        case 1:$info = '<a href="javascript:unrecommend(' . $id . ')">取消推荐</a>';
	            break;
	    }
	    return $info;
	}
	
	function getNodeGroupName($id) {
		if (empty($id)) {
			return '未分组';
		}
		if (isset($_SESSION['nodeGroupList'])) {
			return $_SESSION['nodeGroupList'][$id];
		}
		$Group = D("Group");
		$list = $Group -> getField('id,title');
		$_SESSION['nodeGroupList'] = $list;
		$name = $list[$id];
		return $name;
	}
	
	/**
	 +-------------------------------------------------
	 * 获取用户名, 并缓存数据
	 * @param int $id 用户ID
	 * @return string 返回用户名
	 +-------------------------------------------------
	 */
	function getAuthorName($id){
		if ($list = F('AuthorName')) {
			return $list[$id];
		}
		$dao = D("User");
		$list = $dao->field('id,nickname')->select();
		foreach ($list as $vo) {
			$nameList[$vo['id']] = $vo['nickname'];
		}
		$name = $nameList[$id];
		F('AuthorName', $nameList);
		return $name;
	}
	
	function getOffice($id) {
		if ($list = F('Rolelist')) {
			return $list[$id];
		}
		$model = M('Role');
		$list = $model->field('id,name')->select();
		foreach ($list as $vo) {
			$nameList[$vo['id']] = $vo['name'];
		}
		$name = $nameList[$id];
		F('Rolelist', $nameList);
		return $name;
	}
	
	/**
	 * =============================
	 * 获得分组名称
	 * @param INT $id 分组ID
	 * =============================
	 */
	function getGroupName($id) {
		if ($id == 0) {
			return '无上级组';
		}
		if ($list = F('groupName')) {
			return $list[$id];
		}
		$dao = D("Role");
		$list = $dao -> field('id,name')->select();
		foreach ($list as $vo) {
			$nameList[$vo['id']] = $vo['name'];
		}
		$name = $nameList[$id];
		F('groupName', $nameList);
		return $name;
	}
	
	/**
	 * =============================
	 * 获得分类名称
	 * @param INT $id 分类ID
	 * =============================
	 */
	function getCategoryName($id) {
		if ($list = F('categoryName')) {
			return $list[$id];
		}
		$dao = D("Category");
		$list = $dao -> field('id,name')->select();
		foreach ($list as $vo) {
			$nameList[$vo['id']] = $vo['name'];
		}
		$name = $nameList[$id];
		F('categoryName', $nameList);
		return $name;
	}
	
?>